
package RSSReader;

import java.io.*;
import java.util.Vector;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


/**
 *
 * @author Christian
 */
public class serverConnect
{
    // URLS + COMMANDS FUER CONNECT AUF SERVER
    private String rssListURL       = "http://langi.all-nighter.de/index1.php?command=rss_list";
    private String rssLoadFeedURL   = "http://langi.all-nighter.de/index1.php?command=rss_loadfeed";
    private String rssSearchFeedURL   = "http://langi.all-nighter.de/index1.php?command=rss_searchfeeds";
    private String rssAddURL        = "http://langi.all-nighter.de/index1.php?command=rss_addfeed";
    private String rssDelURL        = "http://langi.all-nighter.de/index1.php?command=rss_delfeed";
    private int userID;


    // USER ID SETZEN
    public void setUserID(int userid)
    {
        this.userID = userid;
    }


    // RSS FEED ZU SERVER HINZUFÜGEN
    public boolean addRSSFeed(String rssDesscription, String rssURL)
    {
        rssDesscription = this.replace(" ", "%20", rssDesscription);

        httpRequest rssRequest = new httpRequest();
        String rssRequ = rssRequest.fetchSite(this.rssAddURL + "&feedName=" + rssDesscription + "&feedURL=" + rssURL + "&userID=" + this.userID);

        if(rssRequ.length() == 5)
            return false;

        else
            return true;
    }


    // RSS FEED VON SERVER LÖSCHEN
    public boolean delRSSFEed(String rssID)
    {
        httpRequest rssRequest = new httpRequest();
        String rssRequ = rssRequest.fetchSite(this.rssDelURL + "&feedID=" + rssID + "&userID=" + this.userID);

        return true;
    }


    // LISTE ALLER RSS FEEDS ZIEHEN
    public Vector getRSSList()
    {
        Vector rssListElms = new Vector();
        
        httpRequest rssRequest = new httpRequest();
        String rssRequ = rssRequest.fetchSite(this.rssListURL + "&userID=" + this.userID);
        String[] rssURLS = split(rssRequ, "#--#");
        for (int i = 0; i < rssURLS.length; i++)
        {
            String[] singleFeed = split(rssURLS[i], "##");

            rssListElms.addElement(singleFeed);
        }

        return rssListElms;
    }
    

    // EINZELNES RSS FEED ZIEHEN
    public Vector getRSSFeed(String rssID)
    {
        Vector rssFeed = new Vector();

        httpRequest rssRequest = new httpRequest();
        String rssRequ = rssRequest.fetchSite(this.rssLoadFeedURL + "&rssID=" + rssID + "&userID=" + this.userID);

        String[] rssURLS = split(rssRequ, "#--#");

        for (int i = 0; i < rssURLS.length; i++)
        {
            String[] singleFeed = split(rssURLS[i], "##");

            rssFeed.addElement(singleFeed);
        }

        return rssFeed;
    }


    // RSS FEEDS SUCHEN
    public Vector searchRSSFeed(String search)
    {
        Vector rssFeed = new Vector();

        httpRequest rssRequest = new httpRequest();
        String rssRequ = rssRequest.fetchSite(this.rssSearchFeedURL + "&rssSearch=" + search + "&userID=" + this.userID);

        String[] rssURLS = split(rssRequ, "#--#");

        for (int i = 0; i < rssURLS.length; i++)
        {
            String[] singleFeed = split(rssURLS[i], "##");

            rssFeed.addElement(singleFeed);
        }

        return rssFeed;
    }

    
    // STRING NACH SEPERATOR ZERLEGEN
    private String[] split(String original, String separator)
    {
        Vector nodes = new Vector();

        // NODES
        int index = original.indexOf(separator);
        while(index>=0)
        {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        nodes.addElement( original );

        // ARRAY ERSTELLEN
        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 )
        {
            for(int loop=0; loop<nodes.size(); loop++)
            {
                result[loop] = (String)nodes.elementAt(loop);
            }
        }

        return result;
    }


    private String replace(String needle, String replacement, String haystack)
    {
        String result = "";
        int index = haystack.indexOf(needle);
        if(index==0)
        {
            result = replacement+haystack.substring(needle.length());
            return replace(needle, replacement, result);
        }
        else if(index>0)
        {
            result = haystack.substring(0,index)+ replacement +haystack.substring(index+needle.length());
            return replace(needle, replacement, result);
        }
        else
            return haystack;
    }
}
